//
//  ForumViewController.m
//  Blank
//
//  Created by 周星宇 on 15/9/25.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ForumViewController.h"
#import "AFNetWorkingGet.h"
#import "ForumTableCell.h"
#import "ForumReplyViewController.h"
#import "LocalSave.h"

@interface ForumViewController () <UITableViewDataSource, UITableViewDelegate, ForumTableCellDelegate>

@property (nonatomic, strong) NSMutableArray *forumArray;
@property (nonatomic, strong) NSArray *forumForKeyArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *top;
@property (nonatomic, strong) NSMutableArray *classic;
@property (nonatomic, strong) NSMutableArray *classicSecond;
@property (nonatomic, strong) NSMutableArray *intelligence;
@property (nonatomic, strong) NSMutableArray *neighbor;
@property (nonatomic, strong) NSMutableArray *loveWatch;

@end

@implementation ForumViewController

- (void)dataHandle{
    self.forumForKeyArray = @[@"顶级奢华品牌专区", @"经典奢华品牌", @"经典豪华品牌", @"平易近人的传统品牌", @"爱表族.表.生活", @"智能手表论坛"];
    if ([LocalSave judgeFileExists:@"forumArray"]) {
        self.forumArray = [LocalSave readFileArr:@"forumArray"].mutableCopy;
        [self tableViewSubViews];
        [self.tableView reloadData];
    }
    else{
        self.forumArray = [NSMutableArray array];
        [AFNetWorkingGet GETWithAFNByURL:ForumURL completion:^(id result) {
            if (result != nil) {
                NSArray *subArray = [result objectForKey:@"data"];
                self.top = [NSMutableArray array];
                self.classic = [NSMutableArray array];
                self.classicSecond = [NSMutableArray array];
                self.intelligence = [NSMutableArray array];
                self.neighbor = [NSMutableArray array];
                self.loveWatch = [NSMutableArray array];
                for (NSDictionary *dic in subArray) {
                    switch ([dic[@"fup"] integerValue]) {
                        case 8: [self.top addObject:dic]; break;
                        case 34: [self.classic addObject:dic]; break;
                        case 44: [self.classicSecond addObject:dic]; break;
                        case 236: [self.intelligence addObject:dic]; break;
                        case 56: [self.neighbor addObject:dic]; break;
                        case 108: [self.loveWatch addObject:dic]; break;
                        default: break;
                    }
                }
                self.forumArray = [NSMutableArray arrayWithObjects:self.top, self.classic, self.classicSecond, self.intelligence, self.neighbor, self.loveWatch, nil];
                [LocalSave saveFileOfArr:self.forumArray fileName:@"forumArray"];
                if (self.forumArray.count > 0) {
                    [self tableViewSubView];
                    [self.tableView reloadData];
                }
            }
        }];
    }
}

- (void)tableViewSubView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 * Height, self.view.frame.size.width, self.view.frame.size.height + 47 * Height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = 0;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ForumTableCell class] forCellReuseIdentifier:@"forum"];
}

- (void)tableViewSubViews{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 * Height, self.view.frame.size.width, self.view.frame.size.height - 55 * Height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = 0;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ForumTableCell class] forCellReuseIdentifier:@"forum"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25 * Height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.forumForKeyArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Height * 81.8 * [[self.forumArray objectAtIndex:indexPath.section] count] / 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.forumForKeyArray objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ForumTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"forum"];
    if (self.forumArray.count != 0) {
        cell.forumArr = [self.forumArray objectAtIndex:indexPath.section];
    }
    cell.sendDelegate = self;
    return cell;
}

- (void)sendUrl:(NSString *)url name:(NSString *)name{
    ForumReplyViewController *forumReply = [[ForumReplyViewController alloc] init];
    forumReply.fid = url;
    forumReply.name = name;
    [self.navigationController pushViewController:forumReply animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"论坛";
    [self dataHandle];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNightModel) name:@"nightModelChange" object:nil];
}

- (void) changeNightModel
{
    if ([NightModel getColorModel]) {
        self.tableView.backgroundColor = [UIColor colorWithRed:149 / 255.0 green:149 / 255.0 blue:149 / 255.0 alpha:0.7];
        self.tableView.sectionIndexColor = [UIColor greenColor];
        [self.tableView reloadData];
    }
    else{
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.sectionIndexBackgroundColor = nil;
        self.tableView.sectionIndexColor =nil;
        [self.tableView reloadData];
    }
}

#pragma mark - 重写弹出抽屉方法
- (void) openOrCloseLeftList
{
    [super openOrCloseLeftList];
    [NightModel changeAppPageCount:2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
